<?php
namespace Game\Controller;
use Common\Controller\CommonController;
class AuthorizeController extends CommonController{

    const AppID     =   'wx0dcb83c5892f258b';
    const AppSecret =   'aab64931415e6bb57986cb832518fc98';

    public function index()
    {
        $rawData        =   I('post.rawData','','htmlspecialchars');
        $signature      =   I('post.signature','','htmlspecialchars');
        $encryptedData  =   I('post.encryptedData','','htmlspecialchars');
        $iv             =   I('post.iv','','htmlspecialchars');
        $info           =   $_POST['userInfo'];
        $code           =   I('post.code','','htmlspecialchars');
        $appid          =   I('post.appid','','htmlspecialchars');
        $data           =   array();
        $user_data      =   array();
        $app_data       =   array('wx0dcb83c5892f258b'=>'aab64931415e6bb57986cb832518fc98');
        if (! empty($appid) && ! empty($code))
        {
            if (! empty($info))
            {
                $info   =   json_decode($info,true);
            }

            $AppSecret              =   $app_data[$appid];
            $url = "https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$AppSecret."&js_code=$code&grant_type=authorization_code";

            $return_data = $this->curlget($url);

            if (empty($return_data['openid']))
            {
                $this->returnData($data,$msg='openid获取失败',$code=400);
            }

            $uid            =   0;
            $where          =   array();
            $where['openid']=   $return_data['openid'];
            $where['appid'] =   $appid;
            $field          =   array('uid','nickname','avatarurl','openid');
            $result         =   M('game_user')->where($where)->field($field)->find();

            if ( ! empty($info))
            {
                $user_data['nickname']      =   $info['nickName'];
                $user_data['avatarurl']     =   $info['avatarUrl'];
            }

            if (empty($result))
            {
                $user_data['appid']       =   $appid;
                $user_data['openid']      =   $return_data['openid'];
                $user_data['create_time'] =   time();
                $user_data['login_time']  =   time();
                $user_data['session_key'] =   $return_data['session_key'];
                $uid                      =   M('game_user')->add($user_data);
            }
            else
            {
                $uid                        =   $result['uid'];
                $user_data['login_time']    =   time();
                $user_data['session_key']   =   $return_data['session_key'];
                M('game_user')->where($where)->save($user_data); // 根据条件更新记录
            }

            $data['openid']     =   $return_data['openid'];
            $data['uid']        =   $uid;
            $this->returnData($data,'ok',200);
        }
        else
        {
            $this->returnData($data,$msg='code或game_id不能为空',$code=400);
        }
    }

    /**
     * 服务器方面
     */
    public function server_busy()
    {

    }
}